import cryptoJs from 'crypto-js';
import config from './config';
// AES
const keyValue = config.k1;
const IVValue = 128;
// 加密
export const encryptDes = (message: string, key: string = keyValue, IV: number = IVValue) => {
  const keyHex = cryptoJs.enc.Utf8.parse(key);
  const iv = cryptoJs.enc.Utf8.parse(IV.toString());
  const option = { iv, mode: cryptoJs.mode.ECB, padding: cryptoJs.pad.Pkcs7 };

  const encrypted = cryptoJs.AES.encrypt(message, keyHex, option);
  return encrypted.toString();
};
// 解密
export const decryptDes = (message: string, key: string = keyValue, IV: number = IVValue) => {
  const keyHex = cryptoJs.enc.Utf8.parse(key);
  const iv = cryptoJs.enc.Utf8.parse(IV.toString());
  const decrypted = cryptoJs.AES.decrypt(message, keyHex, {
    iv,
    mode: cryptoJs.mode.ECB,
    padding: cryptoJs.pad.Pkcs7,
  });
  return decrypted.toString(cryptoJs.enc.Utf8);
};
